package com.js.basicutils.video;

// Created by JS on 2021/1/26.

import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

public class TakeVideoUtils {

    /**
     * 拍摄视频
     * @param activity 要拍摄视频的页面
     * @param quality  要拍摄视频的质量 0 ~ 1
     * @param size 要拍摄视频的大小限制 单位：M
     * @param durationLimit 要拍摄视频的时间
     */
    public static void takeVideo(Activity activity, int quality, int durationLimit, int size, int requestCode) {
        takeVideo(activity, null,  quality, durationLimit, size, requestCode);
    }

    /**
     *  拍摄视频
     * @param activity 要拍摄视频的页面
     * @param fragment 要拍摄视频的碎片
     * @param quality 要拍摄视频的质量 0 ~ 1
     * @param size 要拍摄视频的大小限制 单位：M
     * @param durationLimit 要拍摄视频的时间
     */
    public static void takeVideo(Activity activity, Fragment fragment, int quality, int durationLimit, int size, int requestCode) {
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        takeVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, quality);
        takeVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
        takeVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, size*1024*1024);
        if (fragment == null) {
            if (takeVideoIntent.resolveActivity(activity.getPackageManager()) != null) {
                activity.startActivityForResult(takeVideoIntent, requestCode);
            }
        } else {
            FragmentActivity fragmentActivity = fragment.getActivity();
            if (fragmentActivity != null && takeVideoIntent.resolveActivity(fragmentActivity.getPackageManager()) != null) {
                fragmentActivity.startActivityForResult(takeVideoIntent, requestCode);
            }
        }
    }

    /**
     * 选择视频文件
     *
     * @param activity 要选择视频的页面
     * @param requestCode 请求码
     */
    public static void selectVideo(Activity activity, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        activity.startActivityForResult(intent, requestCode);
    }
}
